package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Map;

@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {
    @DubboReference
    private MenuService menuService;
    /**
     * @Author: lenovo
     * @Date: 8:33 2021/1/19
     * @Parms [username]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 获取菜单列表
     */
    @GetMapping("getMenuList")
    public Result getMenuList(String username) {
        HashSet<Map<String, Object>> menuList = menuService.getMenuListByUsername(username);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, menuList);
    }


    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pageResult = menuService.findPage(queryPageBean);
        if (pageResult!=null){
            return new Result(true,MessageConst.QUERY_MENU_SUCCESS,pageResult);
        }
        return new Result(false,MessageConst.QUERY_MENU_FAIL);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        int i = menuService.add(menu);
        if (i==1){
            return new Result(true,MessageConst.ADD_MENU_SUCCESS);
        }
        return new Result(false,MessageConst.ADD_MENU_FAIL);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        Menu menu = menuService.findById(id);
        if (menu!=null){
            return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menu);
        }
        return new Result(false,MessageConst.QUERY_MENU_FAIL);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        int i = menuService.edit(menu);
        if (i==1){
            return new Result(true,MessageConst.EDIT_MENU_SUCCESS);
        }
        return new Result(false,MessageConst.EDIT_MENU_FAIL);
    }

    @PostMapping("/delete")
    public Result delete(Integer id){
        int i = menuService.delete(id);
        if (i==1){
            return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
        }
        return new Result(false,MessageConst.DELETE_MENU_FAIL);
    }
}
